package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Student;
import com.service.StudentService;
import com.util.Conversion;
import com.util.JdbcUtil;
import com.util.PageUtil;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/student")
public class StudentServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	
    protected void addStudent(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
    	request.getRequestDispatcher("addStudent.jsp").forward(request, response);
    }
    
	protected void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		
		StudentService service = new StudentService();
		try {
			Student student = new Student();
			Conversion.convert(student, request);
			service.update(student);
			//查询数据库中学生列表 
			response.sendRedirect("student?action=query");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			request.setAttribute("mess", "修改失败，请稍后在试");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
	}
	
	protected void toupdate(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String sid = request.getParameter("sid");
		String sql="select * from student where sid =? ";
		JdbcUtil jdbc = new JdbcUtil();
		List<Student> stus = jdbc.queryPreparedStatement(sql, Student.class, sid);
		request.setAttribute("stu", stus.get(0));
		request.getRequestDispatcher("updateStudent.jsp").forward(request, response);
		
	}
	
	protected void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String sid = request.getParameter("sid");
		JdbcUtil jdbc = new JdbcUtil();
		String sql="delete from student where sid=?";
		try {
			jdbc.updatePreparedStatement(sql,sid);
			//查询数据库中学生列表 
			response.sendRedirect("student?action=query");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "删除失败，请稍后在试");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}
	
	protected void query(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		try {
			Integer pagesize=5;
			Integer cp = Integer.parseInt(request.getParameter("cp")==null?"1":request.getParameter("cp"));
			String sid = request.getParameter("sid");
			
			StudentService service = new StudentService();
			//调用分页工具类
			String pageTool = PageUtil.getPageTool(request, service.count(sid), cp, pagesize);
			//ctrl+shift+i  查看一段代码的执行结果
			request.setAttribute("stus", service.query(sid, cp, pagesize));
			request.setAttribute("pageTool", pageTool);
			request.getRequestDispatcher("student.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "查询失败，请稍后在试");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}
		
	protected void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		try {
			Student student = new Student();
			Conversion.convert(student, request);
			StudentService service = new StudentService();

			service.add(student);
			//查询数据库中学生列表 
			response.sendRedirect("student?action=query");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "新增学生失败，请检查录入学号是否重复！！");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

}
